#!/usr/bin/python3

import os
import shutil
import wstage4
import wstage4.os_winxp
import subprocess


os.makedirs("./t", exist_ok=True)
wdir = wstage4.WorkDir("./t", reset=True)

s = wstage4.Settings()
s.program_name = "test"
s.verbose_level = 1

ts = wstage4.os_winxp.TargetSettings()
if True:
    ts.arch = wstage4.Arch.X86
    ts.version = wstage4.Version.WINDOWS_XP
    ts.edition = wstage4.Edition.WINDOWS_XP_PROFESSIONAL
    ts.lang = wstage4.Lang.en_US
    ts.product_key = "HCQ9D-TVCWX-X9QRG-J4B2Y-GR2TT"
    installIsoFile = wstage4.os_winxp.CloudInstallationMedia(ts.arch, ts.version, ts.edition, ts.lang, False, local_iso_filepath="./inst.iso", local_virtio_win_iso_filepath="./virtio.iso")
    if not os.path.exists("./inst.iso") or not os.path.exists("./virtio.iso"):
        installIsoFile.download()
    else:
        installIsoFile._bDownloaded = True
    # installIsoFile = wstage4.os_winxp.LocalInstallationMedia("/var/cache/mirrors/oldwin/storage-file/en_windows_xp_professional_with_service_pack_3_x86_cd_vl_x14-73974.iso",
    #                                                          setup_driver_floppy_filepath="./virtio-win_x86.vfd")
else:
    ts.arch = wstage4.Arch.X86_64
    ts.version = wstage4.Version.WINDOWS_XP
    ts.edition = wstage4.Edition.WINDOWS_XP_PROFESSIONAL
    ts.lang = wstage4.Lang.en_US
    ts.product_key = "B66VY-4D94T-TPPD4-43F72-8X4FY"
    installIsoFile = wstage4.os_winxp.LocalInstallationMedia("/var/cache/mirrors/oldwin/storage-file/en_win_xp_pro_x64_with_sp2_vl_x13-41611.iso",
                                                             setup_driver_floppy_filepath="./virtio-win_amd64.vfd")

ts.user_name = "fpemud"
ts.timezone_index = "085"
ts.keyboard_layout = "US"
ts.extra_drivers = []

with wstage4.Builder(s, ts, wdir) as builder:
    print("        - Prepare...")
    builder.action_prepare(installIsoFile)

    print("        - Installing windows...")
    builder.action_install_os()

    print("        - Installing core applications...")
    builder.action_install_applications()

    builder.add_and_run_custom_action("use_black_background", wstage4.os_winxp.custom_actions.SetBackgroundColor(0x000000))
    builder.add_and_run_custom_action("disable_wallpaper", wstage4.os_winxp.custom_actions.DisableWallpaper())

    print("        - Cleaning up...")
    builder.action_cleanup()

    #print("        - Finish...")
    #builder.finish()

if os.path.exists("./t.bak"):
    shutil.rmtree("./t.bak")
subprocess.check_call(["cp", "-r", "./t", "./t.bak"])
